class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> wordListHash(wordList.begin(), wordList.end());
        unordered_set<string> vis;
        if(!wordListHash.count(endWord))
            return 0;
 
        int ret = 1;
        queue<string> q;
        q.push(beginWord);
        vis.insert(beginWord);
        while(!q.empty())
        {
            ++ret;
            int size = q.size();
            while(size--)
            {
                string t = q.front();
                q.pop();
                for(int i = 0; i < t.size(); ++i)
                {
                    for(char ch = 'a'; ch <= 'z'; ++ch)
                    {
                        string tmp = t;
                        tmp[i] = ch;
                        if(wordListHash.count(tmp) && !vis.count(tmp))
                        {
                            if(tmp == endWord)
                                return ret;
                            q.push(tmp);
                            vis.insert(tmp);
                        }
                    }
                }
            }
        }
        return 0;
    }
};